package action.club;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.ClubModel;
import model.dao.LeagueModel;
import model.dto.Club;
import model.dto.League;
import utility.Uploader;
import controller.Action;
import controller.ActionForward;

public class ActionAddClub implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Club dto=new Club();
		ClubModel dao=new ClubModel();
		
		Uploader up=new Uploader(request, "upload"+File.separator+"club");
		int new_index=dao.getLastIndex()+1;
		boolean hasImageFile=up.upload( ""+new_index);
		//get parameters
		int league_id=Integer.parseInt(up.getMutilPathRequest().getParameter("league_id"));
		int manager_id=Integer.parseInt(up.getMutilPathRequest().getParameter("manager_id"));
		String club_name=up.getMutilPathRequest().getParameter("club_name");
		String city=up.getMutilPathRequest().getParameter("city");
		String stadium=up.getMutilPathRequest().getParameter("stadium");
		
		//Setting up dto
		dto.setName(club_name);
		dto.setCity(city);
		dto.setStadium(stadium);
		dto.setLeagueId(league_id);
		dto.setManagerId(manager_id);
		if(hasImageFile){
			dto.setImage(up.getPreFix()+new_index+"."+up.getExtension());//name as sequence
		}else{
		dto.setImage("default.jpg");//default name
		}
		
		
		if(dao.insert(dto))
			System.out.println("Inserted succesfully.");
		else
			System.out.println("Fail inserting!!!");
		
		ActionForward forward=new ActionForward();
		forward.setPath("club.sp");
		forward.setRedirect(true);
		return forward;
	}

}
